package com.shushan.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.android.permission.FloatPermissionUtil;
import com.shushan.base.Constants;
import com.shushan.lockscreen.FreeWarnWindowManager;
import com.shushan.util.DateUtils;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.ToastUtils;


/**
 * @ClassName: FreeWarnReceiver
 * @Desciption: 小孩桌面自由到时提醒广播
 * @author: yichaohua
 * @date: 2018-03-16
 */
public class FreeWarnReceiver extends BroadcastReceiver{

    private static final String TAG = "FreeWarnReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        String extra = intent.getStringExtra(Constants.CLEAR_WARN_DIALOG_KEY);
        Log.i(TAG,"onReceive:" + DateUtils.getNowDate() + " extra=" + extra);

        if(!TextUtils.isEmpty(extra) && extra.equals(Constants.CLEAR_WARN_DIALOG_KEY)){
            FreeWarnWindowManager.getInstance(context).dismissWarnDialog();
        }else if(FloatPermissionUtil.checkPermission(context)){
            boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
            Log.i(TAG,"isChildControl=" + isChildControl);
            if(isChildControl){
                FreeWarnWindowManager.getInstance(context).showWarnDialog();
            }

        }else {
            ToastUtils.showToast(context, Constants.WARN_TIP_FORWARD_TIME + "分钟后桌面将进入关闭状态，请及时存档。",5);
        }
    }

}
